package com.credithc.eagleeye.api.web;
import com.credithc.eagleeye.api.constants.ResponseCodeEnum;
import com.credithc.eagleeye.api.protocol.TraceInfoSearchRes;
import com.credithc.eagleeye.api.service.SettingService;
import com.credithc.eagleeye.api.service.StatService;
import com.credithc.eagleeye.api.service.TraceInfoSearchService;
import com.credithc.eagleeye.api.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年02月07日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@SuppressWarnings("Duplicates")
@Slf4j
@Controller
public class ConsoleApiCtrl extends BaseApiCtrl {
    @Autowired
    private SettingService settingService;

    @Autowired
    private StatService statService;
    @Autowired
    private TraceInfoSearchService traceInfoSearchService;

    @RequestMapping(value = "/console/getEnums", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ResultVo<EnumVo> getEnums(String platformName, String env) {
        ResultVo<EnumVo> resultVo = new ResultVo<>();
        try {
            SettingVo settingVo = settingService.loadSetting();
            EnumVo enumVo = new EnumVo();
            if(StringUtils.isNotBlank(platformName) && StringUtils.isNotBlank(env)) {
                enumVo = this.statService.statEnum(platformName, env);
                PlatformVo platformVo = settingService.loadPlatform(platformName);
                if (platformVo != null) {
                    enumVo.setMethodNameList(platformVo.getMethodNameList());
                }
            }
            enumVo.setEnvList(settingVo.getEnvList());
            enumVo.setResultList(settingVo.getResultList());
            enumVo.setRpcTypeList(settingVo.getRpcTypeList());
            enumVo.setUserInfoTypeList(settingVo.getUserInfoTypeList());
            resultVo.setData(enumVo);
            resultVo.setCode(ResponseCodeEnum.SUCCESS.getValue());
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode(ResponseCodeEnum.ERROR_SYSTEM.getValue());
            resultVo.setMsg(e.getMessage());
        }
        return resultVo;
    }

    @RequestMapping(value = "/console/queryTraceInfo", method = {RequestMethod.POST})
    @ResponseBody
    public ResultVo<List<RpcTraceInfoTreeVo>> queryTraceInfo(@RequestBody TraceInfoSearchConditionVo conditionVo) {
        ResultVo<List<RpcTraceInfoTreeVo>> resultVo=new ResultVo<>();
        try {
            TraceInfoSearchRes traceInfoSearchRes = this.traceInfoSearchService.searchTraceInfoList(conditionVo);
            resultVo.setCode(ResponseCodeEnum.SUCCESS.getValue());
            resultVo.setData(traceInfoSearchRes.getRpcTraceInfoVoList());
            PageVo pageVo = new PageVo();
            pageVo.setTotalCount(traceInfoSearchRes.getTotalCount());
            resultVo.setPage(pageVo);
            resultVo.setMsg("获取TraceInfo成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode(ResponseCodeEnum.ERROR_SYSTEM.getValue());
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }
    @RequestMapping(value = "/console/queryJavaLog", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public ResultVo<List<JavaLogVo>> queryJavaLog(@RequestBody JavaLogSearchConditionVo conditionVo) {
        ResultVo<List<JavaLogVo>> resultVo=new ResultVo();
        try {
            JavaLogSearchRes javaLogSearchRes = this.traceInfoSearchService.searchJavaLogList(conditionVo);
            resultVo.setCode(ResponseCodeEnum.SUCCESS.getValue());
            resultVo.setData(javaLogSearchRes.getJavaLogVoList());
            resultVo.setMsg("获取JavaLog成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setCode(ResponseCodeEnum.ERROR_SYSTEM.getValue());
            resultVo.setMsg(e.getMessage());
        }

        return resultVo;
    }

}
